package com.ugoodtech.mdcc.web.controller;

import com.ugoodtech.mdcc.core.LogConstants;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.exception.VStarException;
import com.ugoodtech.mdcc.core.repository.ChinaDistrictRepository;
import com.ugoodtech.mdcc.core.repository.QuestionPaperRepository;
import com.ugoodtech.mdcc.core.service.NationService;
import com.ugoodtech.mdcc.core.utils.BaseUtil;
import com.ugoodtech.mdcc.web.dto.*;
import com.ugoodtech.mdcc.web.service.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Api(description = "问卷管理接口")
@Controller
@RequestMapping("/questionPaper")
public class QuestionPaperController extends BaseController {

//    @Autowired
//    private QuestionPaperService QuestionQuestionPaperService;

    @Autowired
    private UserService userService;

    @Autowired
    private QuestionPaperRepository QuestionQuestionPaperRepository;

    @Autowired
    private ChinaDistrictRepository chinaDistrictRepository;

    @Autowired
    private NationService nationService;

    @Autowired
    private QuestionPaperService questionPaperService;

    @Autowired
    private QuestionSectionService questionSectionService;

    @Autowired
    private QuestionGroupService questionGroupService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionAnswerOptionService questionAnswerOptionService;

    @ApiOperation("问卷列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "data", response = QuestionPaperResponse.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/paperList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse paperList(
            @ApiParam("问卷分类") @RequestParam QuestionPaper.QuestionPaperCategory category,
            Pageable pageable) {
        isUserLogin();
        Page<QuestionPaper> questionPapers = questionPaperService.queryQuestionPaper(category,pageable);
        return JsonResponse.successResponseWithPageData(BaseUtil.getDtoPage(questionPapers,pageable, QuestionPaperResponse.class));
    }


    @ApiOperation("章节列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "data", response = QuestionSectionResponse.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/sectionList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse sectionList(
            @ApiParam("问卷id") @RequestParam Long paperId,
            Pageable pageable) {
        isUserLogin();
        Page<QuestionSection> questionSections = questionSectionService.queryQuestionSection(paperId,pageable);
        Page<QuestionSectionResponse> questionSectionResponses=BaseUtil.getDtoPage(questionSections,pageable,QuestionSectionResponse.class);
        for(QuestionSectionResponse questionSectionResponse:questionSectionResponses){
           int num=questionGroupService.queryQuestionGroupNum(questionSectionResponse.getSectionId());
            questionSectionResponse.setGroupNum(num);
        }
        return JsonResponse.successResponseWithPageData(questionSectionResponses);
    }


    @ApiOperation("分组列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "data", response = QuestionGroupResponse.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/groupList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse groupList(
            @ApiParam("章节id") @RequestParam Long sectionId,
            Pageable pageable) {
        isUserLogin();
        Page<QuestionGroup> questionGroups = questionGroupService.queryQuestionGroup(sectionId,pageable);
        return JsonResponse.successResponseWithPageData(BaseUtil.getDtoPage(questionGroups, pageable, QuestionGroupResponse.class));
    }


    @ApiOperation("题目列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "data", response = QuestionResponse.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/questionList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse questionList(
            @ApiParam("分组id") @RequestParam Long groupId,
            Pageable pageable) {
        isUserLogin();
        Page<Question> questions = questionService.queryQuestion(groupId,pageable);
        return JsonResponse.successResponseWithPageData(BaseUtil.getDtoPage(questions,pageable, QuestionResponse.class));
    }


    @ApiOperation("选择题选项列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "data", response = QuestionAnswerOptionResponse.class)
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "/optionList", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse optionList(
            @ApiParam("问题id") @RequestParam Long questionId,
            Pageable pageable) {
        isUserLogin();
        Page<QuestionAnswerOption> questionAnswerOptions = questionAnswerOptionService.queryOption(questionId,pageable);
        return JsonResponse.successResponseWithPageData(BaseUtil.getDtoPage(questionAnswerOptions,pageable, QuestionAnswerOptionResponse.class));
    }


    @ApiOperation("添加问卷")
    @RequestMapping(value = "/addPaper", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addPaper(
            @ApiParam("问卷名字") @RequestParam String name,
            @ApiParam("创建原因") @RequestParam String reason,
            @ApiParam("问卷分类") @RequestParam QuestionPaper.QuestionPaperCategory category,
            @ApiParam("版本号") @RequestParam String version
    ) throws VStarException {
        questionPaperService.createQuestionPaper(name,reason,category,version);
        userLogService.addUserLog(this.getCurrentUser(), null,LogConstants.operate_type_add,LogConstants.business_type_paper,null,null);
        return JsonResponse.successResponse();
    }


    @ApiOperation("添加章节")
    @RequestMapping(value = "/addSection", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addSection(
            @ApiParam("大题名称") @RequestParam String name,
            @ApiParam("问卷Id") @RequestParam Long paperId
    ) throws VStarException {
        questionSectionService.createQuestionSection(name,paperId);
        userLogService.addUserLog(this.getCurrentUser(),null, LogConstants.operate_type_add,LogConstants.business_type_paper,null,null);
        return JsonResponse.successResponse();
    }


    @ApiOperation("添加分组")
    @RequestMapping(value = "/addGroup", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addGroup(
            @ApiParam("题目名称") @RequestParam String name,
            @ApiParam("章节Id") @RequestParam Long sectionId
    ) throws VStarException {
        questionGroupService.createQuestionGroup(name,sectionId);
        userLogService.addUserLog(this.getCurrentUser(),null, LogConstants.operate_type_add,LogConstants.business_type_paper,null,null);

        return JsonResponse.successResponse();
    }


    @ApiOperation("添加填空题目")
    @RequestMapping(value = "/addTextQuestion", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addTextQuestion(
            @ApiParam("分组id") @RequestParam Long groupId,
//            @ApiParam("章节id") @RequestParam Long sectionId,
            @ApiParam("是否必填") @RequestParam boolean require,
            @ApiParam("标题") @RequestParam String title,
            @ApiParam("备注信息") @RequestParam(required = false) String desc
    ) throws VStarException {
        Question question=questionService.createTextQuestion(null,groupId,require,title,desc);
        userLogService.addUserLog(this.getCurrentUser(),null, LogConstants.operate_type_add,LogConstants.business_type_question,null,null);
        return JsonResponse.successResponseWithData(question);
    }


    @ApiOperation("添加上传图片题目")
    @RequestMapping(value = "/addTextPhotoQuestion", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addTextPhotoQuestion(
            @ApiParam("分组id") @RequestParam Long groupId,
//            @ApiParam("章节id") @RequestParam Long sectionId,
            @ApiParam("是否必填") @RequestParam boolean require,
            @ApiParam("标题") @RequestParam String title,
            @ApiParam("备注信息") @RequestParam(required = false) String desc
    ) throws VStarException {
        Question question=questionService.createTextPhotoQuestion(null,groupId,require,title,desc);
        userLogService.addUserLog(this.getCurrentUser(),null, LogConstants.operate_type_add,LogConstants.business_type_question,null,null);
        return JsonResponse.successResponseWithData(question);
    }


    @ApiOperation("添加选择题")
    @RequestMapping(value = "/addChooseQuestion", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addChooseQuestion(
//            @ApiParam("章节id")  @RequestParam Long sectionId,
            @ApiParam("分组id")  @RequestParam(required = false) Long groupId,
            @ApiParam("多选/单选") @RequestParam boolean multiChoose,
            @ApiParam("标准答案")  @RequestParam(required = false) String standardAnswer,
            @ApiParam("是否必填")  @RequestParam boolean require,
            @ApiParam("标题")  @RequestParam(required = false) String title,
            @ApiParam("备注信息")  @RequestParam(required = false) String desc) throws VStarException {
        Question question=questionService.createChooseQuestion(null,groupId,multiChoose,standardAnswer,require,title,desc);
        userLogService.addUserLog(this.getCurrentUser(),null, LogConstants.operate_type_add,LogConstants.business_type_question,null,null);
        return JsonResponse.successResponseWithData(question);
    }

    @ApiOperation("添加选项")
    @RequestMapping(value = "/addAnswer", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse addAnswer(
            @ApiParam("问题Id")  @RequestParam Long questionId,
            @ApiParam("选项文字")  @RequestParam(required = false) String text,
            @ApiParam("允许填空")  @RequestParam boolean addText,
            @ApiParam("结果文字") @RequestParam(required = false) String resultText,
            @ApiParam("建议文字")  @RequestParam(required = false) String suggestText) {
        QuestionAnswerOption questionAnswerOption=questionAnswerOptionService.createAnswer(questionId,text,addText,resultText,suggestText);
        userLogService.addUserLog(this.getCurrentUser(),null, LogConstants.operate_type_add,LogConstants.business_type_question,null,null);
        return JsonResponse.successResponseWithData(questionAnswerOption);
    }

    public static List<Nation> getNationList(NationService nationService, String[] codes){
        List<Nation> tList=new ArrayList<>();
        for(String code:codes){
            Nation nation= nationService.findByCode(code);
            tList.add(nation);
        }
        return tList;
    }
}
